package de.godlike.services.internal;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * This class offers some common information about the running OS. It does not
 * matter whether it's Windows or Unix.
 * 
 * @author asmodaeus
 * 
 */
public class SysAccessCommon {

	private final Logger logger = Logger.getLogger(SysAccessCommon.class);
	/**
	 * Property-key for architecture
	 */
	private final String PROP_KEY_ARCH = "amd64";
	/**
	 * Property-key for OS-type
	 */
	private final String PROP_KEY_TYPE = "os.name";
	/**
	 * Property-key for OS-version
	 */
	private final String PROP_KEY_VERSION = "os.version";
	/**
	 * Property-key for current timezone
	 */
	private final String PROP_KEY_TIMEZONE = "user.timezone";

	/**
	 * @return the cpu-architecture
	 */
	public String getArch() {
		String returnVal = System.getProperty(this.PROP_KEY_ARCH);
		if (returnVal == null) {
			return "Unknown";
		}
		return returnVal;
	}

	/**
	 * @return the OS-Type (name)
	 */
	public String getOsType() {
		String returnVal = System.getProperty(this.PROP_KEY_TYPE);
		if (returnVal == null) {
			return "Unknown";
		}
		return returnVal;
	}

	/**
	 * @return whether this system runs Windows
	 */
	public boolean isWindows() {
		if (this.getOsType().indexOf("Windows") != -1) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * @return the OS-Version
	 */
	public String getOsVersion() {
		String returnVal = System.getProperty(this.PROP_KEY_VERSION);
		if (returnVal == null) {
			return "Unknown";
		}
		return returnVal;
	}

	/**
	 * @return the current timezone
	 */
	public String getTimeZone() {
		String returnVal = System.getProperty(this.PROP_KEY_TIMEZONE);
		if (returnVal == null) {
			return "Unknown";
		}
		return returnVal;
	}

	/**
	 * @return the name of the system
	 */
	public String getSystemName() {
		try {
			return java.net.InetAddress.getLocalHost().getHostName();
		} catch (UnknownHostException e) {
			this.logger.error("getSystemName error: " + e.getMessage(), e);
			return "Unkown";
		}
	}

	/**
	 * @return the current IP-address of the system
	 * @throws UnknownHostException
	 */
	public String getSystemIp() throws UnknownHostException {

		try {
			InetAddress addrs[] = InetAddress.getAllByName(InetAddress
					.getLocalHost().getHostName());
			for (InetAddress addr : addrs) {
				if (addr.isSiteLocalAddress()) {
					return addr.getHostAddress();
				}
			}
			return "Unkown";
		} catch (UnknownHostException e) {
			this.logger.error("getSystemIp error: " + e.getMessage(), e);
			return "Unkown";
		}
	}

	/**
	 * @param dir
	 *            in which the number of files should be count
	 * @return the number of files in the given dir or 0, if the dir is not
	 *         valid
	 */
	public int listDirCount(String dir) {
		if (dir != null) {
			File dir2List = new File(dir);
			if (dir2List.isDirectory()) {
				return dir2List.listFiles().length;
			}
		}
		return 0;
	}

	/**
	 * @param dir
	 *            2 list.
	 * @return a list of File-Objects found under the given directory
	 */
	public List<de.godlike.services.internal.dataTypes.File> listDir(String dir) {
		if (dir != null) {
			File dir2List = new File(dir);
			if (dir2List.isDirectory()) {
				List<de.godlike.services.internal.dataTypes.File> returnList = new ArrayList<de.godlike.services.internal.dataTypes.File>();
				java.io.File[] fileList = dir2List.listFiles();
				for (java.io.File file : fileList) {
					returnList.add(Util.convertJavaIoFile2GodlikeFile(file));
				}
				return returnList;
			}
		}
		return new ArrayList<de.godlike.services.internal.dataTypes.File>();
	}

	/**
	 * @param dir
	 *            2 list.
	 * @return a list of Files (only names) found under the given directory
	 */
	public List<String> listDirOnlyFileNames(String dir) {
		if (dir != null) {
			File dir2List = new File(dir);
			if (dir2List.isDirectory()) {
				List<String> returnList = new ArrayList<String>();
				java.io.File[] fileList = dir2List.listFiles();
				for (java.io.File file : fileList) {
					returnList.add(file.getName());
				}
				return returnList;
			}
		}
		return new ArrayList<String>();
	}

	/**
	 * @deprecated Just 4 Test!
	 * @param args
	 */
	@Deprecated
	public static void main(String[] args) {
		List<de.godlike.services.internal.dataTypes.File> files = new SysAccessCommon()
				.listDir("E:\\");
		for (de.godlike.services.internal.dataTypes.File file : files) {
			System.out.println(file.getAbsolutePath());
		}
	}
}
